package models

import (
	"fly/schema"
	"fmt"
	"github.com/beego/beego/v2/client/orm"
)

// Airport 机场
type Airport struct {
	BaseModel
	Name         string `orm:"column(name);description(机场名)" json:"name"`
	Province     string `orm:"column(province);description(省份名)" json:"province"`
	ProvinceCode string `orm:"column(province_code);description(省份Code)" json:"provinceCode"`
	City         string `orm:"column(city);description(城市名)" json:"city"`
	CityCode     string `orm:"column(city_code);description(城市Code)" json:"cityCode"`
	TypeDictId   int64  `orm:"column(type_dict_id);description(机场类型对应字典Id)" json:"typeDictId"`
	Enabled      int    `orm:"column(enabled);description(启用状态,<0:无效；>=0:有效)"`
}

func init() {
	orm.RegisterModel(new(Airport))
}

// SimpleField 简略查询字段
func (*Airport) SimpleField() []string {
	return []string{"id", "name", "city", "province", "type_dict_id"}
}

// SearchField 定义模型的可搜索字段
func (*Airport) SearchField() []string {
	return []string{"province"}
}

// WhereField 定义模型可作为条件的字段
func (*Airport) WhereField() []string {
	return []string{"id"}
}

func (m *Airport) TableName() string {
	return "airport"
}

// AddAirport insert a new Airport into database and returns
// last inserted Id on success.
func AddAirport(m *Airport, userId int64) (id int64, err error) {
	o := orm.NewOrm()
	m.Creator = userId
	m.Updater = userId
	id, err = o.Insert(m)
	return
}

// GetAirportById retrieves Airport by Id. Returns error if
// Id doesn't exist
func GetAirportById(id int64) (v *Airport, err error) {
	o := orm.NewOrm()
	v = &Airport{BaseModel: BaseModel{Id: id}}
	if err = o.QueryTable(new(Airport)).Filter("id", id).RelatedSel().One(v); err == nil {
		return v, nil
	}
	return nil, err
}

// UpdateAirportById updates Airport by Id and returns error if
// the record to be updated doesn't exist
func UpdateAirportById(m *Airport, userId int64) (err error) {
	o := orm.NewOrm()
	v := Airport{BaseModel: BaseModel{Id: m.Id}}
	// ascertain id exists in the database
	if err = o.Read(&v); err == nil {
		m.Updater = userId
		var num int64
		if num, err = o.Update(m); err == nil {
			fmt.Println("Number of records updated in database:", num)
		}
	}
	return
}

// DeleteAirport deletes Airport by Id and returns error if
// the record to be deleted doesn't exist
func DeleteAirport(id int64) (err error) {
	o := orm.NewOrm()
	v := Airport{BaseModel: BaseModel{Id: id}}
	// ascertain id exists in the database
	if err = o.Read(&v); err == nil {
		var num int64
		if num, err = o.Delete(&Airport{BaseModel: BaseModel{Id: id}}); err == nil {
			fmt.Println("Number of records deleted in database:", num)
		}
	}
	return
}

// DestroyAirport Destroy Airport by Id and returns error if
func DestroyAirport(id int64, userId int64) (err error) {
	o := orm.NewOrm()
	v := Airport{BaseModel: BaseModel{Id: id}}
	// ascertain id exists in the database
	if err = o.Read(&v); err == nil {
		v.Status = schema.StatusDelete
		v.Updater = userId
		var num int64
		if num, err = o.Update(v, "updater", "status"); err == nil {
			fmt.Println("Number of records updated in database:", num)
		}
	}
	return
}
